//
//  MZLocation.m
//  MZBaseDemo
//
//  Created by Chris Yang on 2016/12/12.
//  Copyright © 2016年 Chris Yang. All rights reserved.
//

#import <CoreLocation/CoreLocation.h>
#import <UIKit/UIKit.h>

#import "MZLocation.h"

#define __isIos8Later         (NSOrderedDescending == [[[UIDevice currentDevice] systemVersion] compare:@"8.0" options:NSNumericSearch])

@interface MZLocation()
<CLLocationManagerDelegate>

@property (nonatomic, strong) CLLocationManager *locationManager;

@end

@implementation MZLocation

- (id)init {

    self = [super init];
    
    if (self) {
        _locationManager = [[CLLocationManager alloc] init];
        [_locationManager setDelegate:self];
        
        if (YES == __isIos8Later) {
            /* 8.0 Later */
            [_locationManager requestWhenInUseAuthorization];
        }
    }
    
    return self;
}

+ (MZLocation *)mzSharedInstance {
 
    static MZLocation *sharedLocation = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedLocation = [[MZLocation alloc] init];
    });
    
    return sharedLocation;
}

+ (CLLocation *)mzSharedLocation {
    return [self mzSharedInstance].locationManager.location;
}

+ (void)mzSharedLocationUpdate {
    [[self mzSharedInstance].locationManager startUpdatingLocation];
}

#pragma mark - CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
    [_locationManager stopUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
}

@end
